(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.addPayIdController", addPayIdController);
	addPayIdController.$inject = [ "$scope", "$q", "$uibModalInstance", "ajaxServiceFactory", "dialogsModule", "layoutConfig", "validatorConfig" ];
	function addPayIdController($scope, $q, $uibModalInstance, ajaxServiceFactory, dialogsModule, layoutConfig, validatorConfig) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("addPayIdLookup"), ajaxService = ajaxServiceFactory.buildAjaxService({
			// TODO from basePage payCcy
			getPayCcyOptions : {
				url : "do/cls/dataList/currencies",
				method : "get"
			},
			getAllocCodeOptions : {
				url : "do/cls/dataList/allocCodeList",
				method : "get"
			}
		});
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;

		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](ctrl.payInfo) !== false) {
				$uibModalInstance.close();
			}
		};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.getAllocCodeOptions = function() {
			var defer = $q.defer();
			ajaxService.getAllocCodeOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};

		ctrl.getPayCcyOptions = function() {
			var defer = $q.defer();
			ajaxService.getPayCcyOptions().then(function(response) {
				defer.resolve(response ? response : {});
			});
			return defer.promise;
		};

		ctrl.getCrdrOptions = function() {
			var options = {
				"data" : [ {
					"label" : "label.global.credit",
					"value" : "C"
				}, {
					"label" : "label.global.debit",
					"value" : "D"
				} ]
			};
			return options;
		};

		ctrl.init = function() {
			ctrl.crdrOptions = ctrl.getCrdrOptions();
			ctrl.payInfo = {
				"crdr" : ctrl.crdrOptions ? ctrl.crdrOptions.data[0].value : "",
				"payCcy" : "",
				"allocCode" : ""
			};
			ctrl.getPayCcyOptions().then(function(response) {
				ctrl.payCcyOptions = response;
				ctrl.payInfo.payCcy = ctrl.payCcyOptions ? ctrl.payCcyOptions.data[0].value : "";
			}, angular.noop);
			ctrl.getAllocCodeOptions().then(function(response) {
				ctrl.allocCodeOptions = response;
				ctrl.payInfo.allocCode = ctrl.allocCodeOptions ? ctrl.allocCodeOptions.data[0].value : "";
			}, angular.noop);
		};
	}
})(angular);
